﻿using System.Collections.Generic;
using System.IO;
using Microsoft.Win32;

namespace JoinBoxCurrency
{
    public class AcadDirs
    { 
        public List<AcadProductKey> Dirs { get;}
        /// <summary>
        /// 通过注册表获取本机所有cad的安装路径
        /// </summary>
        public AcadDirs()
        {
            Dirs = new List<AcadProductKey>();

            RegistryKey Adsk = Registry.CurrentUser.OpenSubKey(@"Software\Autodesk\AutoCAD");
            if (Adsk == null)
            {
                return;
            }
            foreach (string ver in Adsk.GetSubKeyNames())
            {
                try
                {
                    RegistryKey emnuAcad = Adsk.OpenSubKey(ver);
                    var curver = emnuAcad.GetValue("CurVer");
                    if (curver == null)
                    {
                        continue;
                    }
                    string app = curver.ToString();
                    string fmt = @"Software\Autodesk\AutoCAD\{0}\{1}";
                    emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
                    if (emnuAcad == null)
                    {
                        fmt = @"Software\Wow6432Node\Autodesk\AutoCAD\{0}\{1}";
                        emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
                    }

                    var acadLocation = emnuAcad.GetValue("AcadLocation");
                    if (acadLocation == null)
                    {
                        continue;
                    }
                    string location = acadLocation.ToString();
                    if (File.Exists(location + "\\acad.exe"))
                    {
                        var produ = emnuAcad.GetValue("ProductName");
                        if (produ == null)
                        {
                            continue;
                        }
                        string productname = produ.ToString();
                        var release = emnuAcad.GetValue("Release");
                        if (release == null)
                        {
                            continue;
                        }
                        string[] strVer = release.ToString().Split('.');
                        var pro = new AcadProductKey()
                        {
                            ProductKey = emnuAcad,
                            Location = location,
                            ProductName = productname,
                            Version = double.Parse(strVer[0] + "." + strVer[1]),
                        };
                        Dirs.Add(pro);
                    }
                }
                catch { }
            }
        }

        public struct AcadProductKey
        {
            /// <summary>
            /// 注册表位置
            /// </summary>
            public RegistryKey ProductKey;
            /// <summary>
            /// cad安装路径
            /// </summary>
            public string Location;
            /// <summary>
            /// cad名称
            /// </summary>
            public string ProductName;
            /// <summary>
            /// cad版本号 17.1之类的
            /// </summary>
            public double Version;
        }
    }
}
